<?php

/**
 * This class implements an API with partial compatibility with CUrlManager.
 */
class FastUrlManager extends CApplicationComponent
{
  /**
   * @var string path to the FUR configuration file to use
   */
  public $configPath;
  
  /**
   * @var string name of the generated class
   */
  public $className='FUR_Router';
  
	/**
	 * @var boolean whether routes are case-sensitive. Defaults to true. By setting this to false,
	 * the route in the incoming request will be turned to lower case first before further processing.
	 * As a result, you should follow the convention that you use lower case when specifying
	 * controller mapping ({@link CWebApplication::controllerMap}) and action mapping
	 * ({@link CController::actions}). Also, the directory names for organizing controllers should
	 * be in lower case.
	 */
	public $caseSensitive=true;
  
  /**
   * @var string the base URL of the application
   */
  private $_baseUrl;
  
  /**
   * @var FUR Router instance
   */
  protected $_router;
  
  /**
   * Initializes the application component.
   */
  public function init()
  {
    parent::init();
    
    require_once dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR . 'lib' . DIRECTORY_SEPARATOR . 'FUR.php';
    
    if (defined('FUR_DEBUG') && FUR_DEBUG==true)
      @unlink(Yii::app()->runtimePath . DIRECTORY_SEPARATOR . basename($this->configPath) . '.php');
    
    FUR::load(
      Yii::app()->basePath . DIRECTORY_SEPARATOR . $this->configPath,
      Yii::app()->runtimePath . DIRECTORY_SEPARATOR . basename($this->configPath) . '.php',
      $this->className
    );
    
    $this->_router = new $this->className;
  }
  
  /**
   * Constructs a URL.
   * @param string the controller and the action (e.g. article/read)
   * @param array list of GET parameters (name=>value). Both the name and value will be URL-encoded.
   * If the name is '#', the corresponding value will be treated as an anchor
   * and will be appended at the end of the URL. This anchor feature has been available since version 1.0.1.
   * @param string the token separating name-value pairs in the URL. Defaults to '&'.
   * @return string the constructed URL
   */
  public function createUrl($route,$params=array(),$ampersand='&')
  {
  }
  
  /**
   * Parses the user request.
   * @param CHttpRequest the request application component
   * @return string the route (controllerID/actionID) and perhaps GET parameters in path format.
   */
  public function parseUrl($request)
  {
  }
  
	/**
	 * Parses a path info into URL segments and saves them to $_GET and $_REQUEST.
	 * @param string path info
	 */
	public function parsePathInfo($pathInfo)
	{
		if($pathInfo==='')
			return;
    parse_str($pathInfo,$vars);
    foreach ($vars as $name=>$value)
      $_GET[$name]=$_REQUEST[$name]=$value;
  }
  
  /**
   * @return string the base URL of the application (the part after host name and before query string).
   * Otherwise, it will not, and the ending slashes are stripped off.
   */
  public function getBaseUrl()
  {
    if($this->_baseUrl!==null)
      return $this->_baseUrl;
    else
    {
      $this->_baseUrl=Yii::app()->getRequest()->getBaseUrl();
      return $this->_baseUrl;
    }
  }
  
	/**
	 * Sets the base URL of the application (the part after host name and before query string).
	 * This method is provided in case the {@link baseUrl} cannot be determined automatically.
	 * The ending slashes should be stripped off. And you are also responsible to remove the script name
	 * if you set {@link showScriptName} to be false.
	 * @param string the base URL of the application
	 * @since 1.1.1
	 */
	public function setBaseUrl($value)
	{
		$this->_baseUrl=$value;
	}
}
